package cn.gzdf.sboot01.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


@Controller//该注解标识该类为控制类
public class HelloController {

    //@RequestMapping("/hello")//该注解用来配置请求路径资源
    //Spring扫描本方法时，发现本方法需要一个参数response响应对象
    //那么就会去Spring容器中寻找这个对象,如果找到了，赋值给response，
    // 我们这个项目为Web项目
    //运行在Tomcat服务器中，启动本项目时，就会启动tomcat的，并把
    //本项目放在Tomcat中运行，Tomcat是web服务器，一定有请求和响应对象的
    //
   /* public void hello(HttpServletResponse response) throws IOException {
        System.out.println("服务器收到了请求");
        //设置响应类型和字符集
        response.setContentType("text/html;charset=utf-8");
        //通过响应对象获取字符输出流
        PrintWriter writer = response.getWriter();
        //向客户端响应一个句话
        writer.println("<b>hello SpringBoot</b>");
    }*/
    @RequestMapping("/hello")
    public void hello(HttpServletResponse response, HttpServletRequest request) throws IOException, ServletException {
        System.out.println("服务器收到了请求");
        //request.getRequestDispatcher("/hello2").forward(request,response);
        response.sendRedirect("https://www.baidu.com");
    }

    @RequestMapping("/hello2")
    public void hello2(HttpServletResponse response, HttpServletRequest request) throws IOException, ServletException {
        System.out.println("服务器收到了请求");
        //设置响应类型和字符集
        response.setContentType("text/html;charset=utf-8");
        //通过响应对象获取字符输出流
        PrintWriter writer = response.getWriter();
        //向客户端响应一个句话
        writer.println("<b>hello SpringBoot</b>");
    }
}
